package com.baizhi.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@Slf4j
public class FileController {

    @Value("${upload.dir}")
    private String realPah;

    @Value("${server.port}")
    private int port;

    @PostMapping("/file/upload")
    public Map<String, Object> upload(MultipartFile photo) {

        Map<String, Object> map = new HashMap<>();
        log.info("接收到的文件名：" + photo.getOriginalFilename());
        try {
            //1.修改文件名
            String ext = "." + FilenameUtils.getExtension(photo.getOriginalFilename());
            String newFileName = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date())+UUID.randomUUID().toString().replaceAll("-","") +ext;
            //2.文件上传
            photo.transferTo(new File(realPah,newFileName));
            //3.返回头像地址
            String url = "http://localhost:"+port+"/"+newFileName;
            map.put("state","true");
            map.put("url",url);
        }catch (Exception e){
            e.printStackTrace();
            map.put("state","false");
            map.put("msg",e.getMessage());
        }
        return map;
    }
}
